<?php

namespace App\Admin\Controllers;

use App\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
    	
        return Admin::content(function (Content $content) {
              $content->header('用户管理');
              $content->description('列表');
              $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->id('ID')->sortable();
            $grid->mobile('电话');
            $grid->sex('性别')->display(function($sex) {
                return $sex == 1 ? "<span class=\"label label-primary\">男</span>" :"<span class=\"label label-danger\">女</span>";
            });
            $grid->is_evaluate('是否评测')->display(function ($is_evaluate) {
                return $is_evaluate == 1 ? "<span class=\"label label-primary\">是</span>" :"<span class=\"label label-danger\">否</span>";
            });
            $grid->birthday('出生日期');
            $grid->height('身高(cm)');
            $grid->weight('体重(kg)');
            $grid->sport_type('运动态势')->display(function ($is_evaluate) {
               switch ($is_evaluate) {
                   case 1:
                       $sport_type = "极轻体力劳动";
                       break;
                   case 2:
                       $sport_type = "轻体力劳动";
                       break;
                   case 3:
                       $sport_type = "中等体力劳动";
                       break;
                   case 4:
                       $sport_type = "重体力劳动";
                       break;
                   default:
                       $sport_type = "";
                       break;
               }
               return $sport_type;
            });
            $grid->created_at('注册时间');
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ]);

            $grid->disableCreateButton();
            $grid->disableExport();
//            $grid->disableRowSelector();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                $filter->like('mobile', '电话')->mobile();
                $filter->equal('status', '状态')->select(['1' => '男', '2' => '女']);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->switch('status');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
